#!/usr/bin/python3
# -*- coding: utf-8 -*-


import pandas as pd
from datetime        import datetime, timedelta
from config        import quant
black_solidity_list   = quant.black_list

from f1_functions import RankAscending, FilterAfter, filter_generate, do_filter

    
    


def default_handler(df1, df2):
    return df1, df2


# def filter_before(df1, df2):
#     # """
#     filter_factor = 'AdaptBolling_fl_100'
#     # 破下轨不做多
#     df1 = df1[~(df1[filter_factor] == -1)]
#     # 破上轨不做空
#     df2 = df2[~(df2[filter_factor] == 1)]
#     # """

#     df1, df2 = filter_fundingrate(df1, df2)

#     return df1, df2


# def filter_fundingrate(df1, df2):
#     df2 = df2[df2['fundingRate'] > -0.025]

#     feature = ['费率min_fl_24'][0]

#     df2[feature + '升序'] = df2.groupby('candle_begin_time')[feature].apply(
#         lambda x: x.rank(pct=False, ascending=True, method='first'))
#     df2 = df2[(df2[feature + '升序'] >= 2) | (df2[feature] >= -0.01)]

#     feature = ['费率max_fl_24'][0]
#     df1[feature + '降序'] = df1.groupby('candle_begin_time')[feature].apply(
#         lambda x: x.rank(pct=False, ascending=False, method='first'))
#     df1 = df1[(df1[feature + '降序'] >= 2) | (df1[feature] <= 0.01)]

#     return df1, df2




def filter_before(df1, df2):
	df1 = df1.copy()
	df2 = df2.copy()
	try:
		black_list = pd.read_csv('temporary_black_short_list.csv', parse_dates=['release_time'])  # 从本地导入做空临时黑名单
		black_list = black_list['symbol'].values.tolist()
		black_list.extend(black_solidity_list)  # 静态黑名单导入静态黑名单
	except:
		black_list = black_solidity_list  # 如果本地导入错误，则用默认的静态黑名单
	for i in black_list:
		df2 = df2[~df2['symbol'].isin([i])]
	# print(df2)
	# print(len(df2) )
	#  删除做多临时和静态黑名单数据
	try:
		black_list = pd.read_csv('temporary_black_long_list.csv', parse_dates=['release_time'])  # 从本地导入做多临时黑名单
		black_list = black_list['symbol'].values.tolist()
		black_list.extend(black_solidity_list)  # 静态黑名单导入静态黑名单
	except:
		black_list = black_solidity_list  # 如果本地导入错误，则用默认的静态黑名单
	for m in black_list:
		df1 = df1[~df1['symbol'].isin([m])]
	#"""
	filter_factor = 'AdaptBolling_fl_100'
	# 破下轨不做多
	df1 = df1[~(df1[filter_factor] == -1)]
	# 破上轨不做空
	df2 = df2[~(df2[filter_factor] ==  1)]
	#"""

	return df1, df2


def filter_fundingrate(df1, df2):

	df1,df2 = filter_before(df1,df2)

	from functions import get_fundingrate
	# 整合资金费率数据

	fundingrate_data = get_fundingrate()

	df2 = pd.merge(df2,
		fundingrate_data[['candle_begin_time', 'symbol', 'fundingRate']],
		on=['candle_begin_time', 'symbol'], how="left")

	curr_time = datetime.now().replace(minute=0, second=0, microsecond=0)
	st = curr_time.strftime("%Y-%m-%d %H:%M:%S")
	start_time = curr_time - timedelta(hours=1)

	# 资金费率为负不做空
	df2 = df2[df2['fundingRate'] < 0]

	# df_t = df2[df2['candle_begin_time'] >= pd.to_datetime(start_time)]
	# df_t.reset_index(inplace=True)
	# print('\n ***本周期资金费率负币种:*** \n',df_t['symbol'])


	return df1, df2


def filter_provisional_list(df1, df2):
	#  删除做空临时和静态黑名单数据
	try:
		black_list = pd.read_csv('temporary_black_short_list.csv', parse_dates=['release_time'])  # 从本地导入做空临时黑名单
		black_list = black_list['symbol'].values.tolist()
		black_list.extend(black_solidity_list)		# 静态黑名单导入静态黑名单
	except:
		black_list = black_solidity_list   # 如果本地导入错误，则用默认的静态黑名单
	for i in black_list:
		df2 = df2[~df2['symbol'].isin([i])]
	# print(df2)
	#  删除做多临时和静态黑名单数据
	try:
		black_list = pd.read_csv('temporary_black_long_list.csv', parse_dates=['release_time'])  # 从本地导入做多临时黑名单
		black_list = black_list['symbol'].values.tolist()
		black_list.extend(black_solidity_list)		# 静态黑名单导入静态黑名单
	except:
		black_list = black_solidity_list  # 如果本地导入错误，则用默认的静态黑名单
	for m in black_list:
		df1 = df1[~df1['symbol'].isin([m])]
	# print(df1)
	return df1, df2